package com.mischa.atc.tests.clearances;

import junit.framework.Assert;

import org.junit.Test;

import com.mischa.atc.Aircraft;
import com.mischa.atc.Clearance;
import com.mischa.atc.Flight;
import com.mischa.atc.clearances.AltitudeClearance;

public class AltitudeClearanceTest {

	@Test
	public void AltitudeClearanceSetsTargetAltitudeAndIsImmediatelyCompleted() {
		Aircraft aircraft = new Aircraft();
		aircraft.setSpeed(250);
		aircraft.setAltitude(5000);

		Flight flight = new Flight(aircraft);

		Clearance clearance = new AltitudeClearance(13000);
		flight.clear(clearance, true);
		
		Assert.assertEquals(5000, flight.getAltitude());
		Assert.assertFalse(clearance.isComplete());

		flight.update(60);

		Assert.assertEquals(13000, flight.getClearedAltitude());
		// Aircraft started climbing, but is not yet at the final altitude		
		Assert.assertTrue(aircraft.getAltitude() > 5000);
		Assert.assertFalse(13000 == aircraft.getAltitude());
		Assert.assertTrue(clearance.isComplete());

		// At 2000 ft/min, climbing 8000 ft should take 4 minutes
		// We have already simulated one, so ...
		flight.update(190);

		// We cannot maintain exact altitude, but should be within 25 ft
		Assert.assertTrue(Integer.toString(aircraft.getAltitude()), Math.abs(13000 - aircraft.getAltitude()) <= 25);
	}

}
